package com.wwdx.service.impl.upms;

import com.google.common.collect.Maps;
import com.wwdx.model.upms.UpmsPermission;
import com.wwdx.model.upms.UpmsRole;
import com.wwdx.model.upms.UpmsUser;
import com.wwdx.service.upms.UpmsApiServiceI;
import com.wwdx.service.upms.UpmsPermissionServiceI;
import com.wwdx.service.upms.UpmsRoleServiceI;
import com.wwdx.service.upms.UpmsUserServiceI;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/2/9 17:40
 */
@Service
public class UpmsApiServiceImpl implements UpmsApiServiceI{

    @Resource
    private UpmsUserServiceI upmsUserService;
    @Resource
    private UpmsRoleServiceI upmsRoleService;
    @Resource
    private UpmsPermissionServiceI upmsPermissionService;

    /**
     * 根据用户id获取所拥有的权限
     * @param upmsUserId 用户ID
     * @return 权限列表
     */
    @Override
    @Cacheable(value = "wwdx-upms-rpc-service-ehcache", key = "'selectUpmsPermissionByUpmsUserId_' + #upmsUserId")
    public List<UpmsPermission> selectUpmsPermissionByUpmsUserId(Integer upmsUserId) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("upmsUserId",upmsUserId);
        String sql = "select permission_id, system_id, pid, name, type, permission_value, uri,status\t" +
                "from upms_permission up where up.status=1 and up.permission_id in (\n" +
                "         select permission_id from upms_role_permission urp where urp.role_id in (\n" +
                "         select uur.role_id role_id from upms_user_role uur where uur.user_id=:upmsUserId\n" +
                "         )\n" +
                "         union\n" +
                "         select permission_id from upms_user_permission uup1 where uup1.user_id=:upmsUserId and uup1.type=1\n" +
                "         )\n" +
                "         and up.permission_id not in (\n" +
                "         select permission_id from upms_user_permission uup2 where uup2.user_id=:upmsUserId and uup2.type=-1\n" +
                "         )";
        return upmsPermissionService.findBySqlToListBean(sql,UpmsPermission.class,params);
    }

    /**
     * 根据用户id获取所属的角色
     * @param upmsUserId 用户ID
     * @return 角色列表
     */
    @Override
    @Cacheable(value = "wwdx-upms-rpc-service-ehcache", key = "'selectUpmsRoleByUpmsUserId_' + #upmsUserId")
    public List<UpmsRole> selectUpmsRoleByUpmsUserId(Integer upmsUserId) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("upmsUserId",upmsUserId);
        String sql = "select role_id, name, title, description\n" +
                "from upms_role ur where ur.role_id in (\n" +
                "select uur.role_id from upms_user_role uur where uur.user_id=:upmsUserId)";
        return upmsRoleService.findBySqlToListBean(sql,UpmsRole.class,params);
    }

    /**
     * 获取UpmsUser
     * @param account 用户账户
     * @return 用户信息
     */
    @Override
    public UpmsUser selectUpmsUserByUsername(String account) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("account",account);
        return upmsUserService.getByHql("from UpmsUser where account=:account", params);
    }
}